﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;

namespace YArchitech.LIB
{
	public class XMLOperating
	{
		public static XmlDocument LoadXml(string path, string rootNode)
		{
			XmlDocument xmlDocument = new XmlDocument();
			if (!File.Exists(path))
			{
				XmlNode newChild = xmlDocument.CreateXmlDeclaration("1.0", "UTF-8", null);
				xmlDocument.AppendChild(newChild);
				XmlElement newChild2 = xmlDocument.CreateElement("", rootNode, "");
				xmlDocument.AppendChild(newChild2);
				try
				{
					xmlDocument.Save(path);
				}
				catch (Exception ex)
				{
					throw ex;
				}
				xmlDocument.Load(path);
			}
			else
			{
				xmlDocument.Load(path);
			}
			return xmlDocument;
		}

		public static XmlDocument LoadXml(string path)
		{
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(path);
			return xmlDocument;
		}

		public static bool SaveXml(XmlDocument xmlDoc, string pathName)
		{
			bool result = false;
			try
			{
				xmlDoc.Save(pathName);
				result = true;
			}
			catch (Exception)
			{
				throw;
			}
			return result;
		}

		public static List<string> GetAllNodes(XmlDocument xmlDoc, string rootNode)
		{
			List<string> list = new List<string>();
			try
			{
				foreach (object obj in xmlDoc.SelectSingleNode(rootNode).ChildNodes)
				{
					XmlNode xmlNode = (XmlNode)obj;
					list.Add(xmlNode.Name);
				}
			}
			catch (Exception)
			{
				throw;
			}
			return list;
		}

		public static List<XmlElement> GetAllElements(XmlDocument xmlDoc, string rootNode)
		{
			List<XmlElement> list = new List<XmlElement>();
			try
			{
				foreach (object obj in xmlDoc.SelectSingleNode(rootNode).ChildNodes)
				{
					XmlNode xmlNode = (XmlNode)obj;
					list.Add(xmlNode as XmlElement);
				}
			}
			catch (Exception)
			{
				throw;
			}
			return list;
		}

		public static List<XmlElement> GetAllElements(XmlDocument xmlDoc, XmlNode xmlNode)
		{
			List<XmlElement> list = new List<XmlElement>();
			try
			{
				foreach (object obj in xmlNode.ChildNodes)
				{
					XmlNode xmlNode2 = (XmlNode)obj;
					list.Add(xmlNode2 as XmlElement);
				}
			}
			catch (Exception)
			{
				throw;
			}
			return list;
		}

		public static XmlNode GetXmlNodeByAttribute(XmlDocument xmlDoc, string rootNode, string name, string value)
		{
			XmlNode xmlNode = xmlDoc.SelectSingleNode(rootNode);
			try
			{
				foreach (object obj in xmlNode.ChildNodes)
				{
					XmlNode xmlNode2 = (XmlNode)obj;
					if ((xmlNode2 as XmlElement).GetAttribute(name) == value)
					{
						return xmlNode2;
					}
				}
			}
			catch (Exception)
			{
				throw;
			}
			return null;
		}

		public static List<string> GetAllNodesText(XmlDocument xmlDoc, string nodePath)
		{
			List<string> list = new List<string>();
			try
			{
				foreach (object obj in xmlDoc.SelectSingleNode(nodePath).ChildNodes)
				{
					XmlNode xmlNode = (XmlNode)obj;
					list.Add(xmlNode.InnerText);
				}
			}
			catch (Exception)
			{
				throw;
			}
			return list;
		}

		public static XmlElement AddElement(XmlDocument xmlDoc, string pathName, string rootNode, string nodeName)
		{
			XmlElement result = null;
			try
			{
				XmlNode xmlNode = xmlDoc.SelectSingleNode(rootNode);
				XmlElement xmlElement = xmlDoc.CreateElement(nodeName);
				xmlNode.AppendChild(xmlElement);
				XMLOperating.SaveXml(xmlDoc, pathName);
				result = xmlElement;
			}
			catch (Exception)
			{
				throw;
			}
			return result;
		}

		public static XmlElement AddorCreatElement(XmlDocument xmlDoc, string pathName, string rootNode)
		{
			XmlElement result = null;
			try
			{
				XmlNode xmlNode = xmlDoc.SelectSingleNode(rootNode);
				if (xmlNode == null)
				{
					XmlElement xmlElement = xmlDoc.CreateElement(rootNode);
					xmlDoc.AppendChild(xmlElement);
					xmlDoc.Save(pathName);
					result = xmlElement;
				}
				else
				{
					result = (XmlElement)xmlNode;
				}
			}
			catch (Exception)
			{
				throw;
			}
			return result;
		}

		public static void AddAttribute(XmlElement xmlElem, string attName, string attValue)
		{
			xmlElem.SetAttribute(attName, attValue);
		}

		public static string GetAttribute(XmlDocument xmlDoc, string rootNode, string attName)
		{
			string result = "";
			try
			{
				result = ((XmlElement)xmlDoc.SelectSingleNode(rootNode)).GetAttribute(attName);
			}
			catch (Exception)
			{
				throw;
			}
			return result;
		}

		public static void SetAttribute(string pathName, string rootNode, string attName, string attValue)
		{
			XmlDocument xmlDocument = XMLOperating.LoadXml(pathName, rootNode);
			((XmlElement)xmlDocument.SelectSingleNode(rootNode)).SetAttribute(attName, attValue);
			XMLOperating.SaveXml(xmlDocument, pathName);
		}

		public static bool AddAttribute(string pathName, string nodeRoot, string attName, string attValue)
		{
			bool result;
			try
			{
				((XmlElement)XMLOperating.LoadXml(pathName, nodeRoot).SelectSingleNode(nodeRoot)).SetAttribute(attName, attValue);
				result = true;
			}
			catch (Exception)
			{
				result = false;
			}
			return result;
		}

		public static void UpdateElement(string path, string node_root, string new_text, string att_name, string att_value)
		{
			XmlDocument xmlDocument = XMLOperating.LoadXml(path, node_root);
			foreach (object obj in xmlDocument.SelectSingleNode(node_root).ChildNodes)
			{
				XmlElement xmlElement = (XmlElement)((XmlNode)obj);
				if (xmlElement.GetAttribute(att_name) == att_value)
				{
					xmlElement.InnerText = new_text;
					xmlDocument.Save(path);
					break;
				}
			}
		}

		public static void deleteNode(string path, string node_root, string att_name, string att_value)
		{
			XmlDocument xmlDocument = XMLOperating.LoadXml(path, node_root);
			XmlNodeList childNodes = xmlDocument.SelectSingleNode(node_root).ChildNodes;
			XmlNode xmlNode = xmlDocument.SelectSingleNode(node_root);
			foreach (object obj in childNodes)
			{
				XmlElement xmlElement = (XmlElement)((XmlNode)obj);
				if (xmlElement.GetAttribute(att_name) == att_value)
				{
					xmlElement.RemoveAll();
					xmlNode.RemoveChild(xmlElement);
					xmlDocument.Save(path);
					break;
				}
			}
		}

		public static XmlElement GetXmlElement(string pathName, string rootNode)
		{
			return (XmlElement)XMLOperating.LoadXml(pathName, rootNode).FirstChild.NextSibling;
		}

		public static void CreatXml(string pathName, string onlyNode)
		{
			XmlDocument xmlDocument = new XmlDocument();
			XmlElement newChild = xmlDocument.CreateElement(onlyNode);
			xmlDocument.AppendChild(newChild);
			xmlDocument.Save(pathName);
		}

		public static string GetNodeValue(XmlDocument xmlDoc, string childName, string cValue)
		{
			string result;
			try
			{
				result = (xmlDoc.FirstChild.NextSibling.SelectSingleNode(childName) as XmlElement).GetAttribute(cValue);
			}
			catch (Exception)
			{
				result = "";
			}
			return result;
		}

		public static void SetNodeValue(XmlDocument xmlDoc, string childName, string valueName, string vValue)
		{
			try
			{
				(xmlDoc.FirstChild.NextSibling.SelectSingleNode(childName) as XmlElement).SetAttribute(valueName, vValue);
			}
			catch (Exception)
			{
			}
		}

		public static string GetNodeText(XmlDocument xmlDoc, string parentName, string childName)
		{
			string result;
			try
			{
				XmlNode xmlNode = xmlDoc.FirstChild.NextSibling.SelectSingleNode(parentName);
				if (xmlNode == null)
				{
					result = "";
				}
				else
				{
					XmlNode xmlNode2 = xmlNode.SelectSingleNode(childName);
					if (xmlNode2 == null)
					{
						result = "";
					}
					else
					{
						result = xmlNode2.InnerText;
					}
				}
			}
			catch (Exception)
			{
				result = "";
			}
			return result;
		}

		public static void EditNodeText(string xmlPath, string parentName, string childName, string text)
		{
			try
			{
				XmlDocument xmlDocument = new XmlDocument();
				xmlDocument.Load(xmlPath);
				XmlNode nextSibling = xmlDocument.FirstChild.NextSibling;
				XmlNode xmlNode = nextSibling.SelectSingleNode(parentName);
				if (xmlNode == null)
				{
					XmlElement newChild = xmlDocument.CreateElement(parentName);
					nextSibling.AppendChild(newChild);
					xmlNode = nextSibling.SelectSingleNode(parentName);
				}
				XmlNode xmlNode2 = xmlNode.SelectSingleNode(childName);
				if (xmlNode2 == null)
				{
					XmlElement newChild2 = xmlDocument.CreateElement(childName);
					xmlNode.AppendChild(newChild2);
					xmlNode2 = xmlNode.SelectSingleNode(childName);
				}
				if (xmlNode2 != null)
				{
					xmlNode2.InnerText = text;
				}
				xmlDocument.Save(xmlPath);
			}
			catch (Exception)
			{
			}
		}

		public static string ReadXML(string pathName, string parentNode, string sonNode)
		{
			string innerText;
			try
			{
				XmlDocument xmlDocument = new XmlDocument();
				xmlDocument.Load(pathName);
				innerText = xmlDocument.SelectSingleNode(parentNode).SelectSingleNode(sonNode).InnerText;
			}
			catch (Exception)
			{
				throw;
			}
			return innerText;
		}

		public static void SaveXML(string pathName, string parentNode, string sonNode, string xmlText)
		{
			try
			{
				XmlDocument xmlDocument = new XmlDocument();
				xmlDocument.Load(pathName);
				xmlDocument.SelectSingleNode(parentNode).SelectSingleNode(sonNode).InnerText = xmlText;
				xmlDocument.Save(pathName);
			}
			catch (Exception)
			{
				throw;
			}
		}
	}
}
